package com.chainyoung.component_tmperson.ui.activity.debit;

import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.entity.OwnDebitDetail;
import com.chainyoung.component_tmperson.entity.WalletDebit;
import com.chainyoung.component_tmperson.ui.mvp.contract.OwnWalletDebitDetailContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.OwnWalletDebitDetailPresenter;

import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;

public class OwnWalletDebitDetailActivity extends BaseActivity<OwnWalletDebitDetailPresenter> implements OwnWalletDebitDetailContract.View {

    @BindView(R2.id.tv_pro_type)
    TextView tvCoinType;
    @BindView(R2.id.txt_buy_num)
    TextView txtBuyNum;
    @BindView(R2.id.tv_debit_num)
    TextView tvDebitNum;
    @BindView(R2.id.txt_day_rate)
    TextView txtDayRate;
    @BindView(R2.id.tv_day_rate)
    TextView tvDayRate;
    @BindView(R2.id.txt_repay_method)
    TextView txtRepayMethod;
    @BindView(R2.id.tv_repay_method)
    TextView tvRepayMethod;
    @BindView(R2.id.txt_period)
    TextView txtPeriod;
    @BindView(R2.id.tv_check_period)
    TextView tvCheckPeriod;
    @BindView(R2.id.tv_period)
    TextView tvPeriod;
    @BindView(R2.id.tv_repayed_principal_amount)
    TextView tvRepayedPrincipalAmount;
    @BindView(R2.id.rl_select_purpose_type)
    RelativeLayout rlSelectPurposeType;
    @BindView(R2.id.tv_repayed_interest_amount)
    TextView tvRepayedInterestAmount;
    @BindView(R2.id.rl_check_repay_record)
    RelativeLayout rlCheckRepayRecord;
    @BindView(R2.id.tv_debit_period)
    TextView tvDebitPeriod;
    @BindView(R2.id.tv_confirm_repay)
    TextView tvConfirmRepay;


    private String debitId;
    private WalletDebit walletDebit;
    private boolean isCompletedRepayed;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPersonActivityComponent.builder()
                .appComponent(appComponent)
                .personActivityModule(new PersonActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUI() {
        setCurrentTitle("借贷明细");
        debitId = getIntent().getStringExtra("debitId");
        walletDebit = (WalletDebit) getIntent().getSerializableExtra("walletDebit");
        isCompletedRepayed=getIntent().getBooleanExtra("isCompleteRepayed",false);
        tvConfirmRepay.setVisibility(isCompletedRepayed? View.GONE:View.VISIBLE);
    }

    @Override
    protected void initData() {
        getDetail();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_own_debit_detail;
    }


    private void updateView(OwnDebitDetail data) {
        tvDebitNum.setText(StringUtil.subZeroAndDot(data.getLoans_info().getPrincipal()));
        tvDayRate.setText(String.format("%s%%", StringUtil.subZeroAndDot(data.getLoans_info().getRate())));
        tvDebitPeriod.setText(data.getLoans_info().getPeriod() + data.getLoans_info().getPeriodUnitStr());
        tvPeriod.setText(data.getLoans_info().getPeriod_line());
        tvRepayedPrincipalAmount.setText(StringUtil.subZeroAndDot(data.getRepayment_info().getRepayment_principal()));
        tvRepayedInterestAmount.setText(StringUtil.subZeroAndDot(data.getRepayment_info().getRepayment_interests()));
    }

    private void getDetail() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put(LibConstant.TOKEN, SPUtil.getString(LibConstant.TOKEN));
        map.put("loans_id", debitId);
        mPresenter.getDebitDetail(map);
    }

    @OnClick(R2.id.rl_check_repay_record)
    void checkRepayRecord(){
        Intent intent=new Intent(mContext,WalletDebitRepayRecordActivity.class);
        intent.putExtra("debitId",debitId);
        intent.putExtra("walletDebit",walletDebit);
        mContext.startActivity(intent);
    }

    @OnClick(R2.id.tv_check_period)
    void checkPeriod(){
        //查看合同
        Intent intent=new Intent(mContext,DebitContractActivity.class);
        intent.putExtra("loansId",debitId);
        mContext.startActivity(intent);


    }

    @OnClick(R2.id.tv_confirm_repay)
    void onConfirmRepay() {
        Intent intent=new Intent(mContext,ConfirmRepayDebitActivity.class);
        intent.putExtra("debitId",debitId);
        intent.putExtra("walletDebit", walletDebit);
        mContext.startActivity(intent);
        finish();
    }




    @Override
    public void onGetOwnWalletDebitDetailSuccessed(OwnDebitDetail data) {
        updateView(data);
    }

    @Override
    public void onGetOwnWalletDebitDetailFailed() {

    }

}
